package modele;

import java.awt.Image;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;

import vue.AddressBook;
import vue.ListInterface;

public final class Contact {

	private static final List<Contact> tab = new ArrayList<Contact>();

	private String name;
	private String lastname;
	private String homeAddress;
	private String companyAddress;
	private String personalPhone;
	private String professionalPhone;
	private String homePage;
	private String email;
	private String photo;
	private String group;

	/**
	 * 
	 * @param a the List containing all information of the new contact
	 */
	public Contact(List<String> a) {

		setName(a.get(0));
		setLastname(a.get(1));
		setHomeAddress(a.get(2));
		setCompanyAddress(a.get(3));
		setPersonalPhone(a.get(4));
		setProfessionalPhone(a.get(5));
		setHomePage(a.get(6));
		setEmail(a.get(7));
		setPhoto(a.get(8));
		setGroup(a.get(9));
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the name
	 */
	public String getFirstName() {
		return name;
	}

	/**
	 * @param lastname
	 *            the lastname to set
	 */
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	/**
	 * @return the lastname
	 */
	public String getLastname() {
		return lastname;
	}

	/**
	 * @param homeAddress
	 *            the homeAddress to set
	 */
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
	}

	/**
	 * @return the homeAddress
	 */
	public String getHomeAddress() {
		return homeAddress;
	}

	/**
	 * @param companyAddress
	 *            the companyAddress to set
	 */
	public void setCompanyAddress(String companyAddress) {
		this.companyAddress = companyAddress;
	}

	/**
	 * @return the companyAddress
	 */
	public String getCompanyAddress() {
		return companyAddress;
	}

	/**
	 * @param personalPhone
	 *            the personalPhone to set
	 */
	public void setPersonalPhone(String personalPhone) {
		this.personalPhone = personalPhone;
	}

	/**
	 * @return the personalPhone
	 */
	public String getPersonalPhone() {
		return personalPhone;
	}

	/**
	 * @param professionalPhone
	 *            the professionalPhone to set
	 */
	public void setProfessionalPhone(String professionalPhone) {
		this.professionalPhone = professionalPhone;
	}

	/**
	 * @return the professionalPhone
	 */
	public String getProfessionalPhone() {
		return professionalPhone;
	}

	/**
	 * @param homePage
	 *            the homePage to set
	 */
	public void setHomePage(String homePage) {
		this.homePage = homePage;
	}

	/**
	 * @return the homePage
	 */
	public String getHomePage() {
		return homePage;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param photo
	 *            the photo to set
	 */
	public void setPhoto(String photo) {

		if (photo.equals(""))
			this.photo = "./images/nophoto.jpg";

		else if (!photo.equals("")
				&& !photo.equals("./images/" + name + lastname + ".jpg")) {
			String file = "./images/" + name + lastname + ".jpg";
			try {
				FileChannel fileSource = new FileInputStream(photo)
						.getChannel();
				FileChannel destination = new FileOutputStream(file)
						.getChannel();
				destination.transferFrom(fileSource, 0, fileSource.size());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.photo = file;
		}

		else if (photo.equals("./images/" + name + lastname + ".jpg")) {
			this.photo = photo;
		}
	}

	/**
	 * @param photo
	 *            the new photo
	 */
	public void changePhoto(String photo) {
		String file = "./images/" + name + lastname + ".jpg";
		try {
			FileChannel fileSource = new FileInputStream(photo).getChannel();
			FileChannel destination = new FileOutputStream(file).getChannel();
			destination.transferFrom(fileSource, 0, fileSource.size());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.photo = file;
	}

	/**
	 * @return the photo
	 */
	public String getPhoto() {
		return photo;
	}



	/**
	 * @param group
	 *            the group to set
	 */
	public void setGroup(String group) {
		this.group = group;
	}

	/**
	 * @return the group
	 */
	public String getGroup() {
		return group;
	}

	/**
	 * 
	 * @param first firstname
	 * @param last lastname
	 * @return true if the contact already exists, false otherwise
	 */
	private static boolean checkExist(String first, String last) {
		boolean b = false;
		for (int i = 0; i < tab.size(); ++i) {
			if (tab.get(i).getFirstName().equals(first)
					&& tab.get(i).getLastname().equals(last)) {
				b = true;
			}
		}
		return b;
	}

	/**
	 * 
	 * @return all contacts
	 */
	public static String[] getAll() {
		String[] t = new String[tab.size()];

		for (int i = 0; i < tab.size(); ++i) {
			t[i] = tab.get(i).getLastname() + " " + tab.get(i).getFirstName();
		}
		return t;
	}

	/**
	 * 
	 * @return all family contacts
	 */
	public static String[] getFamily() {
		List<String> g1 = new ArrayList<String>();

		for (int i = 0; i < tab.size(); ++i) {

			if (tab.get(i).getGroup().equals(AddressBook.FAMILY)) {
				g1.add(tab.get(i).getLastname() + " "
						+ tab.get(i).getFirstName());
			}
		}
		String[] family = g1.toArray(new String[g1.size()]);
		return family;
	}

	/**
	 * 
	 * @return all work contacts
	 */
	public static String[] getWork() {
		List<String> g1 = new ArrayList<String>();

		for (int i = 0; i < tab.size(); ++i) {

			if (tab.get(i).getGroup().equals(AddressBook.WORK)) {
				g1.add(tab.get(i).getLastname() + " "
						+ tab.get(i).getFirstName());
			}
		}
		String[] work = g1.toArray(new String[g1.size()]);
		return work;

	}

	/**
	 * 
	 * @return all friend contacts
	 */
	public static String[] getFriend() {
		List<String> g1 = new ArrayList<String>();

		for (int i = 0; i < tab.size(); ++i) {

			if (tab.get(i).getGroup().equals(AddressBook.FRIEND)) {
				g1.add(tab.get(i).getLastname() + " "
						+ tab.get(i).getFirstName());
			}
		}
		String[] friend = g1.toArray(new String[g1.size()]);
		return friend;

	}

	/**
	 * 
	 * @param l the information list about the new contact to add
	 */
	public static void add(List<String> l) {
		if (l.size() > 0 && !l.get(0).equals("") && !l.get(1).equals("")
				&& !checkExist(l.get(0), l.get(1)))
			tab.add(new Contact(l));
	}

	/**
	 * 
	 * @param l the name and firstname of the contact we want to delete
	 */
	public static void delete(List<String> l) {
		int j = -1;
		for (int i = 0; i < tab.size(); ++i) {
			if (l.size() > 0) {
				if (tab.get(i).getFirstName().equals(l.get(0))
						&& tab.get(i).getLastname().equals(l.get(1))) {
					j = i;
				}
			}
		}
		if (j != -1) {
			tab.remove(j);
		}
	}
	
	/**
	 * @return the tab
	 */
	public static List<Contact> getTab() {
		return tab;
	}

	/**
	 * 
	 * @param string the contact's name and firstname
	 * @return the index of the contact or -1
	 */
	private static int search(String string) {
		// TODO Auto-generated method stub
		int i = string.indexOf(" ");
		String lastname = string.substring(0, i);
		String firstname = string.substring(i + 1);

		for (int j = 0; j < tab.size(); ++j) {
			if (tab.get(j).getFirstName().equals(firstname)
					&& tab.get(j).getLastname().equals(lastname)) {
				return j;
			}
		}
		return -1;
	}

	/**
	 * update a contact information
	 * @param t1
	 * @param t2
	 * @param t3
	 * @param t4
	 * @param t5
	 * @param t6
	 * @param t7
	 * @param t8
	 * @param cbGroup
	 * @param l
	 */
	public static void save(JTextField t1, JTextField t2, JTextField t3,
			JTextField t4, JTextField t5, JTextField t6, JTextField t7,
			JTextField t8, JComboBox cbGroup, ListInterface l) {

		int i = Contact.search(t2.getText() + " " + t1.getText());
		tab.get(i).setHomeAddress(t3.getText());
		tab.get(i).setCompanyAddress(t4.getText());
		tab.get(i).setPersonalPhone(t5.getText());
		tab.get(i).setProfessionalPhone(t6.getText());
		tab.get(i).setHomePage(t7.getText());
		tab.get(i).setEmail(t8.getText());
		tab.get(i).setGroup((String) cbGroup.getSelectedItem());
		if (!l.getFile().equals(""))
			tab.get(i).changePhoto(l.getFile());

		l.getFrame().dispose();
	}

	/**
	 * display a contact details
	 * @param t1
	 * @param t2
	 * @param t3
	 * @param t4
	 * @param t5
	 * @param t6
	 * @param t7
	 * @param t8
	 * @param list
	 * @param cbGroup
	 * @param lblphoto
	 */
	public static void display(JTextField t1, JTextField t2, JTextField t3,
			JTextField t4, JTextField t5, JTextField t6, JTextField t7,
			JTextField t8, JList list, JComboBox cbGroup, JLabel lblphoto) {
		// TODO Auto-generated method stub
		if (list.getSelectedIndex() != -1) {
			int index = Contact.search(list.getSelectedValue().toString());

			t1.setText(tab.get(index).getFirstName());
			t2.setText(tab.get(index).getLastname());
			t3.setText(tab.get(index).getHomeAddress());
			t4.setText(tab.get(index).getCompanyAddress());
			t5.setText(tab.get(index).getPersonalPhone());
			t6.setText(tab.get(index).getProfessionalPhone());
			t7.setText(tab.get(index).getHomePage());
			t8.setText(tab.get(index).getEmail());
			cbGroup.setSelectedItem(tab.get(index).getGroup());

			ImageIcon icon = new ImageIcon(tab.get(index).getPhoto());
			Image img = icon.getImage().getScaledInstance(200, 200,
					java.awt.Image.SCALE_SMOOTH);
			lblphoto.setIcon(new ImageIcon(img));
		}
	}

}
